/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: 
 * 系统版本: 1.0.0
 * 开发人员: zhangfb
 * 开发时间: 2019/5/29 12:44
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.hyacinth.aurora.core;

import com.hyacinth.aurora.utils.PropsUtil;
import com.hyacinth.aurora.utils.StringUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Map;
import java.util.Properties;

import static com.hyacinth.aurora.constant.Constant.SystemProperties.APPLICATION_PROPERTIES;

/**
 * 定义属性文件类
 * @author zhangfb
 * @version 1.0.0.1
 * @since JDK 1.8
 */
public class AuroraProperties {

    private static final Log LOGGER = LogFactory.getLog(AuroraProperties.class);

    private static final Properties localProperties = new Properties();

    static {
        try {
            ClassLoader cl = AuroraProperties.class.getClassLoader();
            URL url = (cl != null ? cl.getResource(APPLICATION_PROPERTIES) :
                    ClassLoader.getSystemResource(APPLICATION_PROPERTIES));
            if (url != null) {
                LOGGER.info("Found 'aurora.properties' file in local classpath");
                try (InputStream is = url.openStream()) {
                    localProperties.load(is);
                }
            }
        } catch (IOException ex) {
            LOGGER.info("Could not load 'aurora.properties' file from local classpath: " + ex);
        }
    }

    public static void setProperty(String key, String value) {
        if (value != null) {
            localProperties.setProperty(key, value);
        } else {
            localProperties.remove(key);
        }
    }

    public static String getProperty(String key) {
        String value = localProperties.getProperty(key);
        if (value == null) {
            try {
                value = System.getProperty(key);
            } catch (Throwable ex) {
                LOGGER.debug("Could not retrieve system property '" + key + "': " + ex);
            }
        }
        return value;
    }

    public static void setFlag(String key) {
        localProperties.put(key, Boolean.TRUE.toString());
    }

    public static boolean getFlag(String key) {
        return Boolean.parseBoolean(getProperty(key));
    }

    /**
     * 获取 String 类型的属性值（可指定默认值）
     */
    public static String getProperty(String key, String defaultValue) {
        String property = getProperty(key);
        return StringUtil.isBlank(property) ? defaultValue : property;
    }

    /**
     * 获取 int 类型的属性值
     */
    public static int getInt(String key) {
        return PropsUtil.getNumber(localProperties, key);
    }

    /**
     * 获取 int 类型的属性值（可指定默认值）
     */
    public static int getInt(String key, int defaultValue) {
        return PropsUtil.getNumber(localProperties, key, defaultValue);
    }

    /**
     * 获取指定前缀的相关属性
     */
    public static Map<String, Object> getMap(String prefix) {
        return PropsUtil.getMap(localProperties, prefix);
    }
}
